<?php
class Mrecords extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		$this->tablename = 'yun_records';
	}
	
	// -------------list页使用
	function pagination($tid = 0, $pindex = 0, &$totalcount = 0, $perpage = 10 ,$order="created") {
		$item = new stdClass();
		$startnumber = $pindex * $perpage;
		$this->db->limit($perpage, $startnumber);
		if($tid){
			if($tid == 6){
				$query = $this->db->query('select * from '.$this->tablename.' where category > 4 order by '.$order.' desc  limit '.$startnumber.','.$perpage);
			}elseif($tid == 7){
				$query = $this->db->query('select * from '.$this->tablename.' where feed_type = "album" order by '.$order.' desc  limit '.$startnumber.','.$perpage);
			}elseif($tid == 8){
				$query = $this->db->query('select * from '.$this->tablename.' where is_fuli = 1 order by '.$order.' desc  limit '.$startnumber.','.$perpage);
			}else{
				$query = $this->db->query('select * from '.$this->tablename.' where category = '.$tid.' order by '.$order.' desc  limit '.$startnumber.','.$perpage);
			}
		}else{
			$query = $this->db->query('select * from '.$this->tablename.' order by created desc  limit '.$startnumber.','.$perpage);
		}
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}

		if($tid){
			if($tid == 6){
				$query1 = $this->db->query('select rid from '.$this->tablename.' where category > 4 ');
			}elseif($tid == 7){
				$query1 = $this->db->query('select rid from '.$this->tablename.' where feed_type = "album" ');
			}elseif($tid == 8){
				$query1 = $this->db->query('select rid from '.$this->tablename.' where is_fuli = 1 ');
			}else{
				$query1 = $this->db->query('select rid from '.$this->tablename.' where category = '.$tid);
			}
		}else{
			$query1 = $this->db->query('select rid from '.$this->tablename);
		}
		$totalcount = $query1->num_rows() ;
		$query->free_result();
		$query1->free_result();
		return $item;
	}

	// -------------list页使用
	function search_pagination($srchtxt = '', $pindex = 0, &$totalcount = 0, $perpage = 10 ,$order="created") {
		$item = new stdClass();
		$startnumber = $pindex * $perpage;
		$this->db->limit($perpage, $startnumber);
		$query = $this->db->query('select * from '.$this->tablename.' where title like "%'.$srchtxt.'%" order by '.$order.' desc  limit '.$startnumber.','.$perpage);
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}

		$query1 = $this->db->query('select rid from '.$this->tablename.' where title like "%'.$srchtxt.'%"');
		$totalcount = $query1->num_rows() ;
		$query->free_result();
		$query1->free_result();
		return $item;
	}

	// ------------首页使用
	function get_all($tid=0 ,$order='view_count') {
		$item = new stdClass();
		if($tid>0){
			if($tid == 6){
				$query = $this->db->query('select rid,title,created,view_count from '.$this->tablename.' where category > 4 order by '.$order.' desc  limit 18 ');
			}elseif($tid == 7){
				$query = $this->db->query('select rid,title,created,view_count from '.$this->tablename.' where feed_type = "album" order by '.$order.' desc  limit 36 ');
			}elseif($tid == 8){
				$query = $this->db->query('select rid,title,created,view_count from '.$this->tablename.' where is_fuli = 1 order by '.$order.' desc  limit 18 ');
			}else{
				$query = $this->db->query('select rid,title,created,view_count from '.$this->tablename.' where category = '.$tid.' order by '.$order.' desc  limit 36 ');
			}
		}else{
			$query = $this->db->query('select rid,title,created,view_count from '.$this->tablename.' order by '.$order.' desc limit 36 ');
		}
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}
	
	// --------------尚未采集资源封面到本地的记录
	function get_image_all() {
		$item = new stdClass();
		$query = $this->db->query('select rid,uk,thumburl,cover_thumb from '.$this->tablename.' where is_image = 0 ');
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}

	// ------------list页右栏使用
	function get_hot_all($tid=0 ,$order="view_count") {
		$item = new stdClass();
		if($tid){
			if($tid == 6){
				$query = $this->db->query('select rid,title from '.$this->tablename.' where category > 4 order by '.$order.' desc limit 30 ');
			}elseif($tid == 7){
				$query = $this->db->query('select rid,title from '.$this->tablename.' where feed_type = "album" order by '.$order.' desc  limit 30 ');
			}elseif($tid == 8){
				$query = $this->db->query('select rid,title from '.$this->tablename.' where is_fuli = 1 order by '.$order.' desc  limit 30 ');
			}else{
				$query = $this->db->query('select rid,title from '.$this->tablename.' where category = '.$tid.' order by '.$order.' desc  limit 30 ');
			}
		}else{
			$query = $this->db->query('select rid,title from '.$this->tablename.' order by view_count desc limit 30 ');
		}
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}

	function get_one($rid) {
		$item = new stdClass();
		$query = $this->db->query('select * from '.$this->tablename.' where rid = '.$rid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_user($uid) {
		$item = new stdClass();
		$query = $this->db->query('select * from yun_user where uid = '.$uid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_last($rid) {
		$rid = $rid - 1 ;
		$item = new stdClass();
		$query = $this->db->query('select rid,title from '.$this->tablename.' where rid = '.$rid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_next($rid) {
		$rid = $rid + 1 ;
		$item = new stdClass();
		$query = $this->db->query('select rid,title from '.$this->tablename.' where rid = '.$rid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

    function exist_id($field,$id){
		$item = 0 ;
		$query = $this->db->query('select rid from '.$this->tablename.' where '.$field.' = '.$id);
		if ($query->num_rows() > 0) {
			$item = 1;
		}
		return $item;
	}

    function insert($arr){
		$id = ($arr['feed_type'] == 'share')?$arr['shareid']:$arr['album_id'] ;
		$field = ($arr['feed_type'] == 'share')?'shareid':'album_id' ;
		if(!$this->exist_id($field ,$id)){                                    // 防止重复记录
			$this->db->insert($this->tablename,$arr);
			return  $this->db->insert_id(); 
		}else{
			log_message('error', $arr['title'].'--------['.$field.'-'.$id.']'.' 已存在库里');
		}
    }

    function update($id,$arr)
    {
        $this->db->where('rid',$id);
        $this->db->update($this->tablename,$arr);
    }

    function delete($id){
        $this->db->where('rid',$id);
        $this->db->delete($this->tablename);
    }

}